package com.yami.shop.api.controller;

import cn.hutool.core.collection.CollectionUtil;
import com.yami.shop.bean.app.param.OrderParam;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.Product;
import com.yami.shop.security.util.SecurityUtils;
import com.yami.shop.service.*;
import com.yami.shop.bean.pt.YxPtgl;
import com.yami.shop.bean.pt.YxPtsp;
import com.yami.shop.bean.pt.YxCtjl;
import lombok.AllArgsConstructor;
import org.apache.commons.collections.CollectionUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;
import com.yami.shop.bean.model.Order;
import com.yami.shop.bean.model.User;

import javax.validation.Valid;
import java.util.*;

@RestController
@RequestMapping("/makeUpGroup")
@ApiIgnore
@AllArgsConstructor
public class MakeUpGroupController {

    @Autowired
    private IYxPtglService iYxPtglService;
    @Autowired
    private IYxPtspService iYxPtspService;
    @Autowired
    private IYxCtjlService iYxCtjlService;
    @Autowired
    private UserService userService;
    @Autowired
    private ProductService productService;

    /**
     * 开团
     * */
    @GetMapping("startGroup")
    public ResponseEntity<Order> startGroup(Long ptspid, String muserid, Long goodsid, String remark, Long shopid,Long addrid){
        String userId = SecurityUtils.getUser().getUserId();
        YxPtgl yxPtglParam =new YxPtgl();
        yxPtglParam.setPtspid(ptspid);
        yxPtglParam.setPzid(muserid);
        yxPtglParam.setGoodsid(goodsid);
        yxPtglParam.setStatus("0");//状态 0:拼团中 1：平团成功 2:平团失败
        YxPtsp yxPtsp = iYxPtspService.selectYxPtspById(ptspid);
        List<YxPtgl> yxPtglList = iYxPtglService.getYxPtglList(yxPtglParam);
        if(CollectionUtil.isNotEmpty(yxPtglList)){
           //拼团已经开始，请不要重复开始拼团
            throw new RuntimeException("拼团已经开始，请不要重复拼团");
        }else{
            //封装订单信息
            /* Order order = new Order();
            order.setShopId(shopid);
            order.setProdName(yxPtsp.getGoodsName());
            order.setUserId(userId);*/

            //开团
            YxPtgl yxPtgl = new YxPtgl();
            yxPtgl.setPtspid(ptspid);//拼团商品id
            yxPtgl.setGoodsid(goodsid);//商品id
            yxPtgl.setGoodsimg(yxPtsp.getGoodsImg());
            yxPtgl.setGoodsname(yxPtsp.getGoodsName());
            yxPtgl.setPtrs(1);//开团，设值为1
            yxPtgl.setCtrs(1);//开团 设值为1
            yxPtgl.setStatus("0");
            yxPtgl.setPzid(muserid);
            iYxPtglService.insertYxPtgl(yxPtgl);
            //插入开团记录
            YxCtjl yxCtjl = new YxCtjl();
            yxCtjl.setPtglId(yxPtgl.getPtglid());
            yxCtjl.setUserId(muserid);
            yxCtjl.setGoodsid(goodsid);
            yxCtjl.setGoodsimg(yxPtsp.getGoodsImg());
            yxCtjl.setGoodsname(yxPtsp.getGoodsName());
            yxCtjl.setZfje(yxPtsp.getPtj());
            yxCtjl.setStatus("0");//状态 0:拼团中 1：平团成功 2:平团失败
            iYxCtjlService.insertYxCtjl(yxCtjl);
        }
        return null;
    }

    /**
     * 参团
     * */
    @GetMapping("joinGroup")
    public void joinGroup(String muserid,Long ptspid,Long goodsid,Long ptglid){
        YxPtsp yxPtsp = iYxPtspService.selectYxPtspById(ptspid);
        YxPtgl yxPtgl = iYxPtglService.selectYxPtglById(ptglid);
        int ctrs = yxPtgl.getCtrs();//成团人数
        //添加参团记录
        YxCtjl yxCtjl = new YxCtjl();
        yxCtjl.setPtglId(yxPtgl.getPtglid());
        yxCtjl.setUserId(muserid);
        yxCtjl.setGoodsid(goodsid);
        yxCtjl.setGoodsimg(yxPtsp.getGoodsImg());
        yxCtjl.setGoodsname(yxPtsp.getGoodsName());
        yxCtjl.setZfje(yxPtsp.getPtj());
        yxCtjl.setStatus("0");//状态 0:拼团中 1：平团成功 2:平团失败
        iYxCtjlService.insertYxCtjl(yxCtjl);
        int ptrs = yxPtgl.getPtrs()+1;
        if(ptrs>=ctrs){//拼团成功
            yxPtgl.setStatus("1");//0:拼团中 1：拼团成功 2:平团失败
            yxPtgl.setPtrs(ptrs);
            iYxPtglService.updateYxPtgl(yxPtgl);//修改拼团人数
            iYxCtjlService.updateYxCtjlStatusByPtglid("1",ptglid);//拼团成功
        }else{//修改拼团人数
            yxPtgl.setPtrs(ptrs);
            iYxPtglService.updateYxPtgl(yxPtgl);
        }

    }
    /**
     * 查询参团会员信息
     * */
    @GetMapping("getJoinGroupUser")
    public ResponseEntity<List<User>> getJoinGroupUser(Long ptglid){
        List<YxCtjl>  yxCtjlList = iYxCtjlService.selectYxCtjlListPtglid(ptglid);
        List<User> userList = new ArrayList<>();
        if(CollectionUtil.isNotEmpty(yxCtjlList)){
            for(YxCtjl yxCtjl : yxCtjlList){
                 User user  = userService.getByid(yxCtjl.getUserId());
                 userList.add(user);
            }
        }
        return ResponseEntity.ok(userList);
    }

    /**
     * 查询已经完成拼团的记录
     */
    @GetMapping("/getPtSuccessedList")
    public ResponseEntity<List<YxPtgl>> getPtSuccessedList(){
        List<YxPtgl> yxPtglList = iYxPtglService.getPtSuccessedList();
        if(CollectionUtils.isNotEmpty(yxPtglList)){
            for(YxPtgl yxPtgl:yxPtglList){
                String userId = yxPtgl.getPzid();
                User user = userService.getUserByUserId(userId);
                if(user!=null){
                    yxPtgl.setNickName(user.getNickName());
                    yxPtgl.setPic(user.getPic());
                    yxPtgl.setPhoneNumber(user.getUserMobile());
                }
            }
        }
        return ResponseEntity.ok(yxPtglList);
    }

    /**
     * 根据商品id查询当前商品所有的拼团结果
     */
    @GetMapping("/getPtSuccessedListbyProdId")
    public ResponseEntity<Map> getPtSuccessedListbyProdId(Long prodId){
        HashMap map = new HashMap();
        List<YxPtgl> yxPtglList= iYxPtglService.getPtSuccessedListbyProdId(prodId);
        //获取拼团商品ID
        YxPtsp yxPtsp = iYxPtspService.getYxPtspbyProdId(prodId);
        if(yxPtsp!=null){
            map.put("ptj",yxPtsp.getPtj());
        }
        //判断当前登录人是否有资格拼团
        String userid = SecurityUtils.getUser().getUserId();
        YxCtjl yxCtjl = iYxCtjlService.getYxCtjl(userid,prodId);
        if(yxCtjl!=null){
            map.put("isGroupPeople","0");
        }else{
            map.put("isGroupPeople","1");
        }
        if(CollectionUtils.isNotEmpty(yxPtglList)){
            for(YxPtgl yxPtgl:yxPtglList){
                String userId = yxPtgl.getPzid();
                User user = userService.getUserByUserId(userId);
                if(user!=null){
                    yxPtgl.setNickName(user.getNickName());
                    yxPtgl.setPic(user.getPic());
                    yxPtgl.setPhoneNumber(user.getUserMobile());
                }
            }
        }
        map.put("yxPtglList",yxPtglList);
        return ResponseEntity.ok(map);
    }

    /**
     * 查询拼团信息
     */
    @GetMapping("/getPtStatus")
    public ResponseEntity<Map> getPtStatus(Long ptglid){
        HashMap map = new HashMap();
        //查询拼团状态
        YxPtgl yxPtgl =iYxPtglService.selectYxPtglById(ptglid);
        if(yxPtgl!=null){
            Long prodId = yxPtgl.getGoodsid();
            Product product = productService.getProductByProdId(prodId);
            YxPtsp yxPtsp = iYxPtspService.getYxPtspbyProdId(prodId);
            if(yxPtgl.getStatus().equals("1")){
                map.put("ptStatus",yxPtgl.getStatus());
                if(product!=null){
                    map.put("prodName",product.getProdName());
                    map.put("prodPic",product.getPic());
                    map.put("price",yxPtsp.getPtj());
                }
                //查询拼团用户
                List<User> userList = new ArrayList<User>();
                List<YxCtjl> yxCtjlList = iYxCtjlService.selectYxCtjlListPtglid(ptglid);
                if(CollectionUtils.isNotEmpty(yxCtjlList)){
                    for(YxCtjl yxCtjl:yxCtjlList){
                        String userId = yxCtjl.getUserId();
                        User user = userService.getUserByUserId(userId);
                        userList.add(user);
                    }
                }
                //开团时间
                map.put("ktTime",yxPtgl.getKtsjtime());
                //活动持续时间
                map.put("duration",yxPtsp.getCtyxsj());
                map.put("userList",userList);
            }else if(yxPtgl.getStatus().equals("2")){
                map.put("ptStatus",yxPtgl.getStatus());
                if(product!=null){
                    map.put("prodName",product.getProdName());
                    map.put("prodPic",product.getPic());
                    map.put("price",yxPtsp.getPtj());
                }
                //查询拼团用户
                List<User> userList = new ArrayList<User>();
                List<YxCtjl> yxCtjlList = iYxCtjlService.selectYxCtjlListPtglid(ptglid);
                if(CollectionUtils.isNotEmpty(yxCtjlList)){
                    for(YxCtjl yxCtjl:yxCtjlList){
                        String userId = yxCtjl.getUserId();
                        User user = userService.getUserByUserId(userId);
                        userList.add(user);
                    }
                }
                //开团时间
                map.put("ktTime",yxPtgl.getKtsjtime());
                //活动持续时间
                map.put("duration",yxPtsp.getCtyxsj());
                map.put("userList",userList);
            }else if(yxPtgl.getStatus().equals("0")){
                map.put("ptStatus",yxPtgl.getStatus());
                map.put("peopleNumber",yxPtgl.getCtrs()-yxPtgl.getPtrs());
                if(product!=null){
                    map.put("prodName",product.getProdName());
                    map.put("prodPic",product.getPic());
                    map.put("price",yxPtsp.getPtj());
                }
                //查询拼团用户
                List<User> userList = new ArrayList<User>();
                List<YxCtjl> yxCtjlList = iYxCtjlService.selectYxCtjlListPtglid(ptglid);
                if(CollectionUtils.isNotEmpty(yxCtjlList)){
                    for(YxCtjl yxCtjl:yxCtjlList){
                        String userId = yxCtjl.getUserId();
                        User user = userService.getUserByUserId(userId);
                        userList.add(user);
                    }
                }
                map.put("userList",userList);
                //开团时间
                map.put("ktTime",yxPtgl.getKtsjtime());
                //活动持续时间
                map.put("duration",yxPtsp.getCtyxsj());
            }
        }
        return ResponseEntity.ok(map);
    }


    /**
     * 获取邀请人的团
     */
    @GetMapping("/getInviterGroup")
    public ResponseEntity<Map> getInviterGroup(Long ptglid){
        HashMap map = new HashMap();
        YxPtgl yxPtgl = iYxPtglService.selectYxPtglById(ptglid);
        List<YxPtgl> yxPtglList = iYxPtglService.getPtSuccessedList();
        if(yxPtgl!=null){
            String  pzid = yxPtgl.getPzid();
            Long prodId = yxPtgl.getGoodsid();
            YxPtsp yxPtsp = iYxPtspService.getYxPtspbyProdId(prodId);
            //获取用户昵称
            User user = userService.getUserByUserId(pzid);
            if(user!=null){
                map.put("nickName",user.getNickName());
                map.put("ptPeopleNumber",yxPtgl.getCtrs()-yxPtgl.getPtrs());
                //开团时间
                map.put("ktTime",yxPtgl.getKtsjtime());
                //活动持续时间
                map.put("duration",yxPtsp.getCtyxsj());
                //拼团成功人数
                map.put("succeedGroup",yxPtglList.size());
            }
        }
        return ResponseEntity.ok(map);
    }




}
